﻿using LXYYY.DTO;
using LXYYY.IOrderService;
using LXYYY.OrderDTO;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace LXYYY.OrderService
{
  public  class ShoppingCarService : ApplicationService, IShoppingCarService
    {
        CSRedis.CSRedisClient client = new CSRedis.CSRedisClient("127.0.0.1:6379");
        private readonly IRepository<LXYYY.Models.ShoppingCar, int> _shoppingCars;//购物车表
        private readonly IRepository<LXYYY.Models.MyUserInformation, int> _UserInformation;//会员信息
        private readonly IRepository<LXYYY.Models.MyGoodsInformation, int> _GoodsInformation;//商品信息
        private readonly IRepository<LXYYY.Models.MyGoodsSpecifications, int> _GoodsSpecifications;//规格
        public ShoppingCarService(IRepository<LXYYY.Models.ShoppingCar, int> shoppingCars,
            IRepository<LXYYY.Models.MyUserInformation, int> UserInformation,
            IRepository<LXYYY.Models.MyGoodsInformation, int> GoodsInformation,
            IRepository<LXYYY.Models.MyGoodsSpecifications, int> GoodsSpecifications
            )
        {
            _shoppingCars = shoppingCars;
            _UserInformation = UserInformation;
            _GoodsInformation = GoodsInformation;
            _GoodsSpecifications = GoodsSpecifications;
        }
        [HttpPost,Route("AddCar")]//添加购物车
        public string AddCar(ShoppingCarDTO shoppingCarShowDTO)
        {
            var key = $"Shopping{shoppingCarShowDTO.MemberId}";
            var p = client.Get(key);
            var addlist = new List<ShoppingCarDTO>();
            if (p==null)
            {
                addlist.Add(shoppingCarShowDTO);
                client.Set(key, addlist);
            }
            else
            {
                var list = JsonConvert.DeserializeObject<List<ShoppingCarDTO>>(p);
                var flist = list.Where(x => x.CommodityId == shoppingCarShowDTO.CommodityId).FirstOrDefault();
                if (flist==null)
                {
                    list.Add(shoppingCarShowDTO);
                }
                else
                {
                    flist.ShoppingCartNum += shoppingCarShowDTO.ShoppingCartNum;
                }
                client.Set(key, list);
            }
            return "添加购物车成功";
        }
        //显示购物车
        [HttpGet,Route("CarShow")]
        public async Task<List<AddCarShowDTO>> CarShow(int uid)
        {
            //获取数据
            var userinfolist = await _UserInformation.GetListAsync();
            var goodsinfolist = await _GoodsInformation.GetListAsync();//商品
            var skulist = await _GoodsSpecifications.GetListAsync();//规格

            var key = $"Shopping{uid}";
            var p = client.Get(key);
            if (p==null)
            {
                return null;
            }
            else
            {
                var carlist = JsonConvert.DeserializeObject<List<ShoppingCarDTO>>(p);
                var list = (from c in carlist
                            join g in goodsinfolist on c.CommodityId equals g.Id
                            join s in skulist on c.GoodsSKU equals s.Id
                            select new AddCarShowDTO()
                            {
                                MemberId = c.MemberId,
                                CommodityId = c.CommodityId,
                                EvaluaTime = c.EvaluaTime,
                                GoodsSKU = c.GoodsSKU,
                                InfomationImg = g.InfomationImg,
                                InformationDetaile = g.InformationDetaile,
                                InformationDisplay = g.InformationDisplay,
                                InformationFreight = g.InformationFreight,
                                InformationName = g.InformationName,
                                InformationSalePrice = g.InformationSalePrice,
                                InformationSpecild = g.InformationSpecild,
                                InformationSPromotion = g.InformationSPromotion,
                                InformationStata = g.InformationStata,
                                InformationWeight = g.InformationWeight,
                                MyProperty = g.MyProperty,
                                ShoppingCartNum = c.ShoppingCartNum,
                                SpecificationsDIsplay = s.SpecificationsDIsplay,
                                SpecificationsName = s.SpecificationsName,
                                SpecificationsOrder = s.SpecificationsOrder,
                                SpecificationsTypeId = s.SpecificationsTypeId
                            }).ToList();
                return list;
            }
        }


        //删除购物车数据
        [HttpGet,Route("DelCar")]
        public string DelCar(int uid, string gid)
        {
            var key = $"Shopping{uid}";
            var p = client.Get(key);
            var carlist = JsonConvert.DeserializeObject<List<ShoppingCarDTO>>(p);
            var gids = gid.Split(',');
            var dellist = new List<ShoppingCarDTO>();
            gids.ToList().ForEach(x =>
            {
                var flist = carlist.Where(e => e.CommodityId == Convert.ToInt32(x)).FirstOrDefault();
                //flist.CommodityId = 0;
                //dellist.Add(flist);
                carlist.Remove(flist);
            });
           
            client.Set(key, carlist);
            return "删除成功";
        }
        //结算订单

    }
}
